package com.union.sqlitedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;

import com.union.sqlitedemo.bean.person;
import com.union.sqlitedemo.mySqlite.MyDebemanager;
import com.union.sqlitedemo.mySqlite.MySqilteHelpter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    public static String TAG = "MainActivity";

    private MySqilteHelpter helper;

    private int PageSize = 15;
    private int CurrentPage = 1;
    //总页数
    private int pageNumber;
    //数据总条数
    private int totalNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        helper = MyDebemanager.getInstance(this);
        totalNumber = MyDebemanager.getDataCount(helper.getWritableDatabase(), MySqilteHelpter.TABLE_NAME);
        pageNumber = (int) Math.ceil(totalNumber / PageSize);

        List<person> listByCurrentPager = MyDebemanager.getListByCurrentPager(helper.getWritableDatabase(), MySqilteHelpter.TABLE_NAME, PageSize, CurrentPage);
        Log.d("Debug", "当前的页数为" + listByCurrentPager.size());

        SeekBar seek_pro = findViewById(R.id.seek_pro);
        seek_pro.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Log.d("Debug", "当前的seekbar为" + seekBar.getProgress());
            }
        });

        //加载某个路径的数据库
//        @NonNull String path,   //数据库的路径
//        @Nullable SQLiteDatabase.CursorFactory factory,  游标工厂指定为null
//        int flags  SQLiteDatabase.OPEN_READONLY 只读     SQLiteDatabase.OPEN_READWRIT 可读可写
//        SQLiteDatabase db = SQLiteDatabase.openDatabase("", null, SQLiteDatabase.OPEN_READWRITE);
    }

    public void createDb(View view) {
        Log.d(TAG, "创建数据库点击事件");
    }


    public void onclick(View view) {
        SQLiteDatabase db = helper.getWritableDatabase();
        if (db == null) {
            Log.d("Debug", "db数据库为空--");
            return;
        }
        switch (view.getId()) {
            case R.id.tv_sql_insert:
                String url = "insert into persion(name) values('lisi')";
                MyDebemanager.sqlInsert(db, url);
                db.close();
                break;
            case R.id.tv_sql_update:
                String url1 = "update persion set name='zhangsan' where id=3";
                MyDebemanager.sqlInsert(db, url1);
                db.close();
                break;
            case R.id.tv_sql_delete:
                String url2 = "delete from persion where id=4";
                MyDebemanager.sqlInsert(db, url2);
                db.close();
                break;
            case R.id.tv_sql_select:
                String url10 = "select * from persion";
                Cursor cursor1 = MyDebemanager.SelectDataBySql(db, url10, null);
                ArrayList<person> peopleList = MyDebemanager.CursortoList(cursor1);
                for (person bean : peopleList) {
                    Log.d("Debug", "取出来的数据为" + bean.toString());
                }
                break;
            case R.id.tv_api_insert:
                ContentValues values = new ContentValues();
                values.put("name", "apizengjia");
                //返回的是插入的行数  -1 标识失败
                long result = db.insert("persion", null, values);
                if (result > 0) {
                    Log.d("Debug", "api插入数据成功");
                }
                db.close();
                break;
            case R.id.tv_api_update:
//                String table,
//                ContentValues values,
//                String whereClause,   where的条件 参数名 id  或是完整的where条件  id=1
//                String[] whereArgs    参数的实体  1
                ContentValues values1 = new ContentValues();
                values1.put("name", "api更改");
                int update = db.update("persion", values1, "id=1", null);
                if (update > 0) {
                    Log.d("Debug", "api修改数据成功");
                }
                db.close();
                break;
            case R.id.tv_api_delete:
                int delete = db.delete("persion", "id=3", null);
                if (delete > 0) {
                    Log.d("Debug", "删除数据成功");
                }
                db.close();
                break;
            case R.id.tv_api_select:
//                String table,   //表名字
//                String[] columns, //参数   null  查看所有的参数
//                String selection,   where的条件  查询条件
//                String[] selectionArgs,  where条件的占位符
//                String groupBy,  group by 条件
//                String having,   having 条件
//                String orderBy  order by 条件
                Cursor query = db.query(MySqilteHelpter.TABLE_NAME, null, "id>?", new String[]{"1"}, null, null, null);
                ArrayList<person> people = MyDebemanager.CursortoList(query);
                for (person person : people) {
                    Log.d("Debug", "通过api查看的数据为" + person.toString());
                }
                break;
        }
    }


}